#include "DbStorage/CreateRootRequirementStorageCmd.hpp"
#include "Model/Requirement.hpp"
#include "Model/Package.hpp"
#include "CaseDB/Connection.hpp"
#include "CaseDB/Statement.hpp"
#include <cassert>

namespace DbStorage
{


CreateRootRequirementStorageCmd::CreateRootRequirementStorageCmd( 
		Model::Requirement& subject, CaseDB::Connection& dbConnection )
		: DbStorageCmd( subject, Storage::StorageCommand::Create, dbConnection)
{
	assert( !subject.hasParent());
	assert( NULL != subject.package());
}

// ---------------------------------------------------------------------------------------------------------------------

void CreateRootRequirementStorageCmd::execute()
{
	using namespace Model;
	using namespace CaseDB;

	Requirement& requirement = dynamic_cast< Requirement&>( rSubject_);
	Statement insertStatement( "{?=call CREATE_ROOT_REQUIREMENT( ?, ?, ?)}");
	insertStatement.addParameter( DbTypesEnum::Integer);
	insertStatement.addParameter( DbTypesEnum::Integer, requirement.package()->storedID());
	insertStatement.addParameter( requirement.name());
	insertStatement.addParameter( requirement.statement());

	dbConnection_.execute( insertStatement);

	boost::any packageID = insertStatement.paramsBegin()->value();
	requirement.setStoredID( packageID);
}


} // namespace DbStorage
